import clr
import System
clr.AddReference('Microsoft.Scripting.dll')
clr.AddReference('IronPython.dll')
import IronPython
import Microsoft
import ClrWrapper
from System.Reflection.Emit import OpCodes

def Ldc_I4(ilGenerator, value):
	o = (
		OpCodes.Ldc_I4_M1,
		OpCodes.Ldc_I4_0,
		OpCodes.Ldc_I4_1,
		OpCodes.Ldc_I4_2,
		OpCodes.Ldc_I4_3,
		OpCodes.Ldc_I4_4,
		OpCodes.Ldc_I4_5,
		OpCodes.Ldc_I4_6,
		OpCodes.Ldc_I4_7,
		OpCodes.Ldc_I4_8
		)
	if value > -2 and value < 9:
		ilGenerator.Emit(o[value+1])
	elif value > -129 and value < 128:
		ilGenerator.Emit(OpCodes.Ldc_I4_S, System.SByte(value))
	else:
		ilGenerator.Emit(OpCodes.Ldc_I4, value)

def Ldarg(ilGenerator, index):
	o = (
		OpCodes.Ldarg_0,
		OpCodes.Ldarg_1,
		OpCodes.Ldarg_2,
		OpCodes.Ldarg_3,
		)
	if index >= 0 and index < 4:
		ilGenerator.Emit(o[index])
	else:
		ilGenerator.Emit(OpCodes.Ldarg, index)

def TypeArray(*args):
	types = map(ClrWrapper.GetClrType, args)
	return System.Array[System.Type](types)
